package com.ruoqing.auth.service.impl;


import com.ruoqing.auth.service.AuthRoleService;
import com.ruoqing.auth.service.AuthUserService;
import com.ruoqing.redis.domain.AuthUser;
import com.ruoqing.redis.domain.UserRole;
import com.ruoqing.redis.dto.UserAuthorityDto;
import org.springframework.data.redis.core.RedisConnectionUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
	* @author YaoXian
	* @title: UserDetailsServiceImpl
	* @projectName ruoxi-blog
	* @description: 若兮微服务 - 认证授权中心 - Security重写UserDetailsService实现类
	* @date 2020-11-26 15:19
	*/
@Service
public class UserDetailsServiceImpl implements UserDetailsService {

		@Resource
		private AuthUserService authUserService;
		
		@Resource
		private AuthRoleService authRoleService;
		
		@Resource
		private RedisTemplate<String, UserAuthorityDto> redisTemplate;
		
		public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
				UserAuthorityDto userAuthorityDto = new UserAuthorityDto();
				AuthUser authUser = authUserService.getAuthUserByUserName(username);
				List<GrantedAuthority> grantedAuthorityList = new ArrayList<GrantedAuthority>();
				if (authUser!=null){
						userAuthorityDto.setAuthUser(authUser);
						List<UserRole> userRole = authRoleService.findRoleByUserId(authUser.getId());
						ArrayList<String> roleString = new ArrayList<String>(userRole.size());
						HashMap<String, String> roleMap = new HashMap<String, String>(userRole.size());
						for (UserRole role : userRole) {
								roleString.add(role.getEnname());
								roleMap.put(role.getName(),role.getDescription());
						}
						userAuthorityDto.setRoleMap(roleMap);
						redisTemplate.boundValueOps(username).set(userAuthorityDto,30, TimeUnit.MINUTES);
						if (roleString.contains("super_admin")){
								List<String> allRole = authRoleService.findAllRole();
								for (String role : allRole) {
										GrantedAuthority authority = new SimpleGrantedAuthority("ROLE_"+role);
										grantedAuthorityList.add(authority);
								}
						}else {
								for (String role : roleString) {
										GrantedAuthority authority = new SimpleGrantedAuthority("ROLE_"+role);
										grantedAuthorityList.add(authority);
								}
						}
				}
				if (redisTemplate.getConnectionFactory()!=null){
						RedisConnectionUtils.unbindConnection(redisTemplate.getConnectionFactory());
				}
				return new User(authUser.getUsername(),authUser.getPassword(),grantedAuthorityList);
		}
}
